import axios from 'axios';

//post
export const postRequest = (url, data = {}) => {
	return axios.post(url, data, {
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded'
		}
	}, );
};

//拼接URL和dict
const getRequest = (url, params) => {
	const query = Object.entries(params)
		.map(([key, value]) => `${key}=${(value)}`)
		.join('&');
	const fullUrl = `${url}?${query}`;
	return axios.get(fullUrl);
};

// 拉取角色列表
export const getCharacter = (uid, bPublic) => {
	const url = '/api/get_character_resources/';
	const params = {
		uid,
		bPublic
	};
	return getRequest(url, params);
};

// conversation page 获取图片
export const getPic = (uid, cid, bPublic) => {
	const url = '/api/picture_url/';
	const params = {
		uid,
		cid,
		bPublic
	};
	return getRequest(url, params);
};

// 获取签名
export const getSign = async (uid, cname, bPublic) => {
	const url = '/api/getSign/';
	const params = {
		uid,
		cname,
		bPublic
	};
	try {
		const response = await getRequest(url, params);
		console.log(response.data);
		const {
			cid,
			...credentials
		} = response.data;
		return {
			credentials,
			cid
		};
	} catch (error) {
		console.error('获取签名出错', error);
	}
};


// 上传文件成功后给 server 发 message
export const sendUploadSuccessMessage = (uid, cid, bPublic) => {
	const url = '/api/checkandinsert/';
	const params = {
		uid,
		cid,
		bPublic
	};
	return getRequest(url, params);
};


//更新角色信息getSignForModifyCharacter
export const getSignForModifyCharacter = async (uid, cid, cname, uploadtype) => {
	const url = '/api/getSignForModifyCharacter/';
	const params = {
		uid,
		cid,
		cname,
		uploadtype
	};
	try {
		const response = await getRequest(url, params);
		const credentials = response.data;
		console.log('Credentials:', credentials);
		return credentials;
	} catch (error) {
		console.error('获取签名出错', error);
	}
};

// 上传文件成功后给 server 发 message
export const sendModifySuccessMessage = (uid, cid, uploadtype) => {
	const url = '/api/checkandinsertForModifyCharacter/';
	const params = {
		uid,
		cid,
		uploadtype
	};
	return getRequest(url, params);
};

//检查当日签到
export const checkSignIn = (uid) => {
	const url = '/api/check_sign_in/';
	const params = {
		uid
	};
	return getRequest(url, params);
};

//私有转公有
export const private2public = (uid, cid) => {
	const url = '/api/private2public/';
	const params = {
		uid,
		cid
	};
	return getRequest(url, params);
};

//签到
export const getPointsFromSigned = (uid) => {
	const url = '/api/points_from_signed/';
	const params = {
		uid
	};
	return getRequest(url, params);
};

//获取用户积分
export const getUserPoints = (uid) => {
	const url = '/api/get_user_points/';
	const params = {
		uid
	};
	return getRequest(url, params);
};